package com.cxdr.awsips.distribute.processor;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.cxdr.awsips.common.logger.LogFactory;
import com.cxdr.awsips.common.thread.BaseProcessor;
import com.cxdr.awsips.distribute.bean.Link_Source;
import com.cxdr.awsips.distribute.bean.ReportFileLog;
import com.cxdr.awsips.distribute.bean.Target;
import com.cxdr.awsips.distribute.config.LinkConfig;
import com.cxdr.awsips.distribute.file.FileHelper;
import com.cxdr.awsips.distribute.file.FileHelperFactoy;

public class DistributeWorker extends BaseProcessor{
	private static Logger logger = LogFactory
			.makeNewLogger(DistributeWorker.class);
	private LinkConfig link;
	private String key;
	private Timer scaner;
	private static final int SCAN_TIME = 5000; // 5秒扫描一次
	private boolean started = false;
	private String statu = "stop";
	public static final String TASK_NAME = "Distribute";
	public static final String THREAD_NAME = "DistributeWorker";
	private DistributeWorker(LinkConfig link) {
		this.link = link;
		key = getKey();
		scaner = new Timer();
		setTask_name(TASK_NAME);
		setThread_name(THREAD_NAME);
	}

	public static DistributeWorker getWorker(LinkConfig link) {
		return new DistributeWorker(link);
	}
	
	public String checkStatu(){
		return statu;
	}
	
	public void startWork() {
		if (!isStarted()) {
			logger.info("DistributeWorker: " + key + " start work");
			scaner.schedule(new TimerTask() {
				@Override
				public void run() {
					logger.info("DistributeWorker: " + key + " begin scan");
					OutFileDispatcher fileDispatcher = new OutFileDispatcher(
							link);
					for (Link_Source source : link.getSources()) {
						FileHelper fileHelper = null;
						try {
							fileHelper = FileHelperFactoy.getHelper(source);
							fileHelper.open();
							fileHelper.setLocalDir(link.getLocalDir());
//							fileHelper.setRemoteDir(source.getFolder());
							fileHelper.setKeyTime(link.getKeepTime());
							fileHelper.setSub(link.isHas_folder());
							for (File file : fileHelper.scanDir()) {
								String fileName = file.getName();
								if (StringUtils.endsWithIgnoreCase(fileName,
										".txt")) {
									ReportFileLog reportFileLog = new ReportFileLog(file);
									fileDispatcher.OutPutFileQueue.add(reportFileLog);
									StoreReportProccessor.ReportFileLogQueue
											.add(reportFileLog);
									logger.debug("add OutPutFileQueue file:"
											+ file.getName());
								}
							}
							fileDispatcher.beginProccess();
						} catch (Exception e) {
							logger.error("获取源" + source + "错误", e);
							e.printStackTrace();
							statu = "error";
							link.setStatu(statu);
						} finally {
							fileHelper.close();
						}
					}

				}
			}, 0, SCAN_TIME);
			setStarted(true);
			
		}
	}

	public void stopWork() {
		if (!isStarted()){
			logger.info("DistributeWorker: " + key + " stop work");
			scaner.cancel();
			setStarted(false);
		}
	}

	public String getKey() {
		if (StringUtils.isBlank(key) && link != null) {
			StringBuilder key = new StringBuilder();
			for (Target source : link.getSources()) {
				key.append(source.getId());
			}
			key.append("-");
			for (Target target : link.getTargets()) {
				key.append(target.getId());
			}
			this.key = key.toString();
		}
		return key;
	}

	public LinkConfig getLink() {
		return link;
	}

	public void setLink(LinkConfig link) {
		this.link = link;
	}

	public boolean isStarted() {
		return started;
	}

	public void setStarted(boolean started) {
		if(started){
			statu = "running";
		}else{
			statu = "stoped";
		}
		link.setStatu(statu);
		this.started = started;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
}
